package com.itheima.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;


public class GameJFrame extends JFrame implements KeyListener, ActionListener {

    public static int[][] win = new int[][]{
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    public int[][] data = new int[4][4];
    public int x;
    public int y;
    public int step = 0;
    public String path = "image\\animal\\animal1\\";
    //设置菜单选项的选项
    JMenuItem relogin = new JMenuItem("重新登录");
    JMenuItem replay = new JMenuItem("重新游戏");
    JMenuItem closeGame = new JMenuItem("关闭游戏");
    JMenuItem officialAccount = new JMenuItem("公众号");
    JMenuItem girl = new JMenuItem("美女");
    JMenuItem animal = new JMenuItem("动物");
    JMenuItem sport = new JMenuItem("运动");


    public GameJFrame() throws HeadlessException {


        //初始化界面
        initJFrame();

        //初始化菜单栏
        initMenubar();

        //打乱图片顺序,用数组
        initData();

        //记载图片
        initImage();

        //设置可见
        this.setVisible(true);

    }

    private void initData() {
        int[] data1 = new int[16];
        Random random = new Random();
        for (int i = 0; i < 16; i++) {
            data1[i] = i;
        }
        for (int i = 0; i < data1.length; i++) {
            int pos = random.nextInt(16);
            //交换pos位置和i位置的数据
            int tep = data1[i];
            data1[i] = data1[pos];
            data1[pos] = tep;
        }
        //将打乱后数据放到二维数组中
        for (int i = 0; i < 16; i++) {
            data[i / 4][i % 4] = data1[i];
        }

    }

    private void initImage() {

        //先清空之前加载所有图片
        this.getContentPane().removeAll();


        //判断是否已经通关
        if (isvictory()) {

            //加载win图标
            JLabel winImage = new JLabel(new ImageIcon("image\\win.png"));
            winImage.setBounds(203, 283, 197, 73);
            getContentPane().add(winImage);

        }

        //展示一共走了多少步
        JLabel stepLable = new JLabel("步数: " + step);
        stepLable.setBounds(50, 30, 100, 20);
        getContentPane().add(stepLable);


        //加载16个小图片
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {//快捷键shift+f6
                if (data[i][j] == 0) {
                    x = i;
                    y = j;
                }
                JLabel jLabel = new JLabel(
                        new ImageIcon(path + data[i][j] + ".jpg")
                );
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                jLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
                this.getContentPane().add(jLabel);
            }
        }
        //加载背景图片
        JLabel jLabel = new JLabel(
                new ImageIcon("image\\background.png")
        );
        jLabel.setBounds(40, 40, 508, 560);
        this.getContentPane().add(jLabel);


        //刷新操作
        getContentPane().repaint();

    }

    private void initMenubar() {
        //设置菜单
        JMenuBar jMenuBar = new JMenuBar();
        //设置菜单选项
        JMenu function = new JMenu("功能");
        JMenu about = new JMenu("关于我们");


        //创建更换图片
        JMenu changeImage = new JMenu("更换图片");


        function.add(relogin);
        function.add(replay);
        function.add(closeGame);


        //4.把美女，动物，运动添加到更换图片当中
        changeImage.add(girl);
        changeImage.add(animal);
        changeImage.add(sport);

        about.add(officialAccount);

        function.add(changeImage);

        jMenuBar.add(function);
        jMenuBar.add(about);


        this.setJMenuBar(jMenuBar);

        relogin.addActionListener(this);
        replay.addActionListener(this);
        closeGame.addActionListener(this);
        officialAccount.addActionListener(this);

        girl.addActionListener(this);
        animal.addActionListener(this);
        sport.addActionListener(this);
    }

    private void initJFrame() {
        //设置width,height
        this.setSize(603, 680);
        //设置居中,以及置顶
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("拼图单机版 V-1.0");
        //默认停止运行方式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        //添加keyListener事件监听
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int num = e.getKeyCode();
        if (num == 81) {//显示完整图
            //清空之前图片
            getContentPane().removeAll();
            //加载all.jpg
            JLabel jLabel = new JLabel(new ImageIcon(path + "all.jpg"));
            jLabel.setBounds(83, 134, 420, 420);
            getContentPane().add(jLabel);

            //加载背景图
            //加载背景图片
            JLabel jLabel_b = new JLabel(
                    new ImageIcon("image\\background.png")
            );
            jLabel_b.setBounds(40, 40, 508, 560);
            this.getContentPane().add(jLabel_b);

            //刷新
            getContentPane().repaint();

        }


    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (isvictory()) return;
        int num = e.getKeyCode();
        if (num == 38) {//向上移动
            if (!(x == 3)) {
                data[x][y] = data[x + 1][y];
                data[x + 1][y] = 0;
                x++;
                step++;
                initImage();
                System.out.println("向上移动");
            }
        } else if (num == 40) {
            if (x == 0) return;
            data[x][y] = data[x - 1][y];
            data[x - 1][y] = 0;
            x--;
            step++;
            initImage();
            System.out.println("向下移动");
        } else if (num == 37) {
            if (y == 3) return;
            data[x][y] = data[x][y + 1];
            data[x][y + 1] = 0;
            y++;
            step++;
            initImage();
            System.out.println("向左移动");
        } else if (num == 39) {
            if (y == 0) return;
            data[x][y] = data[x][y - 1];
            data[x][y - 1] = 0;
            y--;
            step++;
            initImage();
            System.out.println("向右移动");
        } else if (num == 81) {//按q键,查看完整图片,松开时需要重新清除加载数据
            initImage();
        } else if (num == 79) {//over,按o键,直接通关
            data = new int[][]{
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 0}
            };
            initImage();
        }
        ;

    }


    public boolean isvictory() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                if (data[i][j] != win[i][j]) return false;
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();

        if (source == relogin) {
            //关闭当前页面
            setVisible(false);
            //加载登录界面
            new LoginJFrame();


        } else if (source == replay) {


            //step步数清零,必须要在initimage之前,否则initimage回先一步调用step变量,出现step不清零的bug
            step = 0;
            //打乱data数据
            initData();
            ///加载图片
            initImage();


        } else if (source == officialAccount) {

            JDialog jDialog = new JDialog();

            //设置大小size
            jDialog.setSize(400, 400);
            //设置弹窗置顶
            jDialog.setAlwaysOnTop(true);
            //设置弹窗居中
            jDialog.setLocationRelativeTo(null);
            //设置弹窗不关闭无法操纵其他界面
            jDialog.setModal(true);
            jDialog.setTitle("记得投币偶");


            JLabel aboutLable = new JLabel(
                    new ImageIcon("image\\about.png")
            );
            aboutLable.setBounds(0, 0, 317, 318);
            jDialog.getContentPane().add(aboutLable);

            //让弹窗显示出来
            jDialog.setVisible(true);

        } else if (source == closeGame) {

            System.exit(0);

        } else if (source == girl) {
            System.out.println("girl");
            Random r = new Random();

            path = "image\\girl\\girl" + (r.nextInt(13) + 1) + "\\";
            //step步数清零,必须要在initimage之前,否则initimage回先一步调用step变量,出现step不清零的bug
            step = 0;
            //打乱data数据
            initData();
            //加载图片
            initImage();


        } else if (source == animal) {
            System.out.println("animal");
            Random r = new Random();

            path = "image\\animal\\animal" + (r.nextInt(8) + 1) + "\\";
            //step步数清零,必须要在initimage之前,否则initimage回先一步调用step变量,出现step不清零的bug
            step = 0;
            //打乱data数据
            initData();
            ///加载图片
            initImage();

        } else if (source == sport) {
            System.out.println("sport");
            Random r = new Random();

            path = "image\\sport\\sport" + (r.nextInt(10) + 1) + "\\";
            //step步数清零,必须要在initimage之前,否则initimage回先一步调用step变量,出现step不清零的bug
            step = 0;
            //打乱data数据
            initData();
            ///加载图片
            initImage();
        }

    }
}
